<?php
/**
* @version		1.0  - 	Joomla 1.5.x
* @package		Component Com Products
* @copyright	Wampvn Group
* @license		GNU/GPL
* @website          http://wampvn.com
* @description    view by category.
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.view');
jimport('joomla.html.pagination');
class ProductViewCategory extends JView
{
	function display($tpl = null)
	{
		global $option, $mainframe;
                
                //same group
                $group = array(
                                'group1'=>array('san-pham-gia-tri-gia-tang','ca-fillet'),
                                'group2'=>array('tom')
                               );
		// config 
		$params =& $mainframe->getPageParameters('com_products');
		
		$limit = 12;

		$layout = JRequest::getVar('layout', 'default');	
		if ($layout == 'default'){
			//$limit = $params->get('limit1', 2);
		} else {
			//$limit = $params->get('limit2', 2);
		}
		$limitstart = JRequest::getVar('limitstart', 0);
		$mid = JRequest::getInt('mid', 0);
		$catid = JRequest::getVar('catid', '');
		$getSize = JRequest::getInt('size', 0);
		$wsize = '&size=0';
		if (!empty($getSize)){
						$wsize = '&size='. $getSize;
		}
		$wmid = '&mid=0';
		if (!empty($mid)){
						$wmid = '&mid='. $mid;
		}
		// get model
		$model = &$this->getModel();
		
		
		$listProduct = $model->getListProduct($limit, $limitstart);
		$info = $model->getInfo();
		$total = $model->getTotal();
		$catName = $model->getCatName();

		$pagination = new JPagination($total, $limitstart, $limit);
		
		$titleCatName = JFilterOutput::stringURLSafe(trim($catName));
		$titleCatName = str_replace(' ', '-', strtolower($titleCatName));		

		// info view product	
		for($i = 0; $i < count($listProduct); $i++) {	
			$row =& $listProduct[$i];
			$title = JFilterOutput::stringURLSafe($row->name);
			$title = str_replace(' ', '-', strtolower($title));
			$row->link = JRoute::_('index.php?option=' . $option .
			'&view=detail&id=' . $row->id.':'.$title);			
			$row->rating = $model->getRating($row->id);
		}
		
		
		// SEO title
		$doc =& JFactory::getDocument();
		$titleSEO = '';
		if (!empty($catName)) {
			$titleSEO = $catName;
			if (!empty($manufacturerName)){
			$titleSEO = $catName . ' | Nhà sản xuất ' . $manufacturerName;
			}
		}
		$doc->setTitle($titleSEO);
		$doc->setDescription('Các Sản phẩm '. $catName .' tại Hungvuongpanga.com - Che bien va san xuat ca basa');
		
		
		$text_price = $params->get('text_price', 'Sắp có hàng');
		// REF Ass
		$this->assignRef('text_price', $text_price);
                
                //change $listProduct => $listProduct group by category
                $listProduct = self::groupCategory($listProduct);
		$this->assignRef('listProduct', $listProduct);
		$this->assignRef('total', $total);
		$this->assignRef('catName', $catName);
		$this->assignRef('titleCatName', $titleCatName);		
		$this->assignRef('catid', $catid);
		$this->assignRef('pagination', $pagination);
		parent::display($tpl);
	}
        
        //group category
        public static function groupCategory($listProduct){
            $arrProductByCat = array();
            
            foreach($listProduct as $p){
                //cẩn thận: catname phải có alias #
                $arrProductByCat[self::strToAlias($p->catname)][] = $p;
            }
            
            return $arrProductByCat;
        }
        
        public static function strToAlias($string){
             $trans = array(
                'à'=>'a','á'=>'a','ả'=>'a','ã'=>'a','ạ'=>'a',
                'ă'=>'a','ằ'=>'a','ắ'=>'a','ẳ'=>'a','ẵ'=>'a','ặ'=>'a',
                'â'=>'a','ầ'=>'a','ấ'=>'a','ẩ'=>'a','ẫ'=>'a','ậ'=>'a',
                'À'=>'a','Á'=>'a','Ả'=>'a','Ã'=>'a','Ạ'=>'a',
                'Ă'=>'a','Ằ'=>'a','Ắ'=>'a','Ẳ'=>'a','Ẵ'=>'a','Ặ'=>'a',
                'Â'=>'a','Ầ'=>'a','Ấ'=>'a','Ẩ'=>'a','Ẫ'=>'a','Ậ'=>'a',    
                'đ'=>'d','Đ'=>'d',
                'è'=>'e','é'=>'e','ẻ'=>'e','ẽ'=>'e','ẹ'=>'e',
                'ê'=>'e','ề'=>'e','ế'=>'e','ể'=>'e','ễ'=>'e','ệ'=>'e',
                'È'=>'e','É'=>'e','Ẻ'=>'e','Ẽ'=>'e','Ẹ'=>'e',
                'Ê'=>'e','Ề'=>'e','Ế'=>'e','Ể'=>'e','Ễ'=>'e','Ệ'=>'e',
                'ì'=>'i','í'=>'i','ỉ'=>'i','ĩ'=>'i','ị'=>'i',
                'Ì'=>'i','Í'=>'i','Ỉ'=>'i','Ĩ'=>'i','Ị'=>'i',
                'ò'=>'o','ó'=>'o','ỏ'=>'o','õ'=>'o','ọ'=>'o',
                'ô'=>'o','ồ'=>'o','ố'=>'o','ổ'=>'o','ỗ'=>'o','ộ'=>'o',
                'ơ'=>'o','ờ'=>'o','ớ'=>'o','ở'=>'o','ỡ'=>'o','ợ'=>'o',
                'Ò'=>'o','Ó'=>'o','Ỏ'=>'o','Õ'=>'o','Ọ'=>'o',
                'Ô'=>'o','Ồ'=>'o','Ố'=>'o','Ổ'=>'o','Ỗ'=>'o','Ộ'=>'o',
                'Ơ'=>'o','Ờ'=>'o','Ớ'=>'o','Ở'=>'o','Ỡ'=>'o','Ợ'=>'o',
                'ù'=>'u','ú'=>'u','ủ'=>'u','ũ'=>'u','ụ'=>'u',
                'ư'=>'u','ừ'=>'u','ứ'=>'u','ử'=>'u','ữ'=>'u','ự'=>'u',
                'Ù'=>'u','Ú'=>'u','Ủ'=>'u','Ũ'=>'u','Ụ'=>'u',
                'Ư'=>'u','Ừ'=>'u','Ứ'=>'u','Ử'=>'u','Ữ'=>'u','Ự'=>'u',
                'ỳ'=>'y','ý'=>'y','ỷ'=>'y','ỹ'=>'y','ỵ'=>'y',
                'Y'=>'y','Ỳ'=>'y','Ý'=>'y','Ỷ'=>'y','Ỹ'=>'y','Ỵ'=>'y'
            );
            return str_replace(" ","_",strtr($string, $trans));
        }
	
	function checkImage($proid){
		$model = &$this->getModel();
		$imgDefault = $model->getImageDefault($proid);
		return $imgDefault->filename;
	}
	
	function getImages($id) {
		$model = &$this->getModel();
		$imgs = $model->getImages($id);
		return $imgs;
	}
	
}
?>